var app = {
	description : 'App Common JS Library',
	version : '2.00.0'
};

app.webroot = function() {

	if (webroot == null) {
		return "";
	}
	return webroot;

};

app.common = {
	getElementById : function(objId) {

		if (document.getElementById(objId)) {
			return document.getElementById(objId);
		}

		if (window.parent && window.parent.document.getElementById(objId)) {
			return window.parent.document.getElementById(objId);
		}

		if (window.parent.parent
				&& window.parent.parent.document.getElementById(objId)) {
			return window.parent.parent.document.getElementById(objId);
		}

		if (window.parent.parent
				&& window.parent.parent.parent.document.getElementById(objId)) {
			return window.parent.parent.parent.document.getElementById(objId);
		}

		return null;

	},

	// 重置表单方法
	// FormId:Form的ID
	clearForm : function(FormId) {
		$('#' + FormId).form('clear');
	},
	resetForm : function(FormId) {
		$('#' + FormId)[0].reset();
	},
	window : {
		openWindow : function(url, winDesc) {
			window
					.open(
							url,
							winDesc,
							'top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no');
		}
	}

};

// AJAX获取JSON数据
// onSuccess:正确时执行的函数
// onFailure:错误时执行的函数
app.ajax = {
	post : function(url, data, msgRender, onSuccess, onFailure) {
		if (msgRender) {
			app.messager.clear(msgRender);
		}

		var layerIndex = layer.load(2);

		setTimeout(function() {
			$.ajax({
				url : app.random.randUrl(url),
				dataType : "json",
				data : data,
				async : false,
				cache : false,
				type : "POST",
				success : function(resp, textStatus) {

					layer.close(layerIndex);

					if (resp && (resp.success || ('true' == resp.success))) {

						if (onSuccess) {
							onSuccess(resp);
						} else {
							if (msgRender) {
								app.messager.success(msgRender);
							} else {
								app.messager.openSuccess();

							}

						}

					} else {
						if (onFailure) {
							onFailure(resp);
						} else {
							if (msgRender) {
								app.messager.error(msgRender, resp.message);
							} else {
								app.messager.openError(resp.message);
							}
						}

					}
				}
			});
		}, 500);

		/**
		 * jQuery.postJSON(url, data, function(resp) {
		 * 
		 * if (resp && (resp.success || ('true' == resp.success))) {
		 * 
		 * if (onSuccess) { onSuccess(resp); } } else { if (onFailure) {
		 * onFailure(resp); } } });
		 */

	}
};

// 格式化日期：年-月-日
app.inputDate = {
	myformatter : function(date) {
		var y = date.getFullYear();
		var m = date.getMonth() + 1;
		var d = date.getDate();
		return y + '-' + (m < 10 ? ('0' + m) : m) + '-'
				+ (d < 10 ? ('0' + d) : d);
	},

	myparser : function(s) {
		if (!s)
			return new Date();
		var ss = (s.split('-'));
		var y = parseInt(ss[0], 10);
		var m = parseInt(ss[1], 10);
		var d = parseInt(ss[2], 10);
		if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
			return new Date(y, m - 1, d);
		} else {
			return new Date();
		}
	},
	myformatterTime : function(date) {
		var y = date.getFullYear();
		var m = date.getMonth() + 1;
		var d = date.getDate();
		var h = date.getHours();
		var i = date.getMinutes();
		var s = date.getSeconds();
		return y + '-' + (m < 10 ? ('0' + m) : m) + '-'
				+ (d < 10 ? ('0' + d) : d) + ' ' + (h < 10 ? ('0' + h) : h)
				+ ':' + (i < 10 ? '0' + i : i) + ':' + (s < 10 ? '0' + s : s);
	}
};

app.layer = {

	closeSelf : function() {
		var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
		parent.layer.close(index); // 再执行关闭
	}

};

app.button = {
	enable : function(btnId) {
		$("#" + btnId).removeClass("disabled");
		$("#" + btnId).removeAttr("disabled");

	},

	disable : function(btnId) {
		$("#" + btnId).addClass("disabled");
		$("#" + btnId).attr('disabled', "true");
	}
};

app.messager = {
	clear : function(renderId) {
		$("#" + renderId).html("");
	},
	success : function(renderId, successMsg) {

		var html = "<div class='sui-msg msg-large msg-block msg-success'>";
		html += "<div class='msg-con'>操作成功";

		if (successMsg) {
			html += "," + successMsg;
		}

		html += "<button type='button' data-dismiss='msgs' class='sui-close'>×</button>";
		html += "</div>";
		html += "<s class='msg-icon'></s>";
		html += "</div>";
		$("#" + renderId).html(html);

	},
	error : function(renderId, errorMsg) {

		var html = "<div class='sui-msg msg-large msg-block msg-error'>";
		html += "<div class='msg-con'>操作失败：" + errorMsg;
		html += "<button type='button' data-dismiss='msgs' class='sui-close'>×</button>";
		html += "</div>";
		html += "<s class='msg-icon'></s>";
		html += "</div>";
		$("#" + renderId).html(html);
	},

	openSuccess : function() {

		var html = "<div class='sui-msg msg-large msg-block msg-success'>";
		html += "<div class='msg-con'>操作成功</div>";
		html += "<s class='msg-icon'></s>";
		html += "</div>";

		layer.open({
			type : 1,
			title : false,
			closeBtn : 1,
			shadeClose : true,
			skin : 'layer-open-msg',
			area : [ '300px', '80px' ],
			content : html
		});
	},
	
	openSuccessMsg : function(successMsg) {

		var html = "<div class='sui-msg msg-large msg-block msg-success'>";
		html += "<div class='msg-con'>"+successMsg+"</div>";
		html += "<s class='msg-icon'></s>";
		html += "</div>";

		layer.open({
			type : 1,
			title : false,
			closeBtn : 1,
			shadeClose : true,
			skin : 'layer-open-msg',
			area : [ '300px', '80px' ],
			content : html
		});
	},
	
	openError : function(errorMsg) {
		var html = "<div class='sui-msg msg-large msg-block msg-error'>";
		html += "<div class='msg-con'>操作失败：" + errorMsg;
		html += "</div>";
		html += "<s class='msg-icon'></s>";
		html += "</div>";

		layer.open({
			type : 1,
			title : false,
			closeBtn : 1,
			shadeClose : true,
			skin : 'layer-open-msg',
			area : [ '350px', '60px' ],
			content : html
		});
	}
};

/*
 * 在父级标签打开窗口
 */
app.tabs = {
	parentOpen : function(title1, fileName) {
		if (window.parent.document.getElementById("parent_tt")) {
			window.parent.open1(title1, fileName);
		} else if (window.parent.parent.document.getElementById("parent_tt")) {
			window.parent.parent.open1(title1, fileName)
		} else {
			window.open(fileName);
		}
	}
};

app.random = {

	next : function(begin, end) {
		if (!begin) {
			begin = 1000000000;
		}

		if (!end) {
			end = 99999999999;
		}

		return Math.floor(Math.random() * (end - begin)) + begin;
	},
	randUrl : function(url) {
		var rand = app.random.next();

		if (url.indexOf("?") > 0) {
			url += "&randomInteger=" + rand;
		} else {
			url += "?randomInteger=" + rand;
		}

		return url;
	}

};

app.checkbox = {
	uncheck : function(checkbox) {
		for (var i = 0; i < checkbox.length; i++) {
			var cb = $(checkbox[i]).checkbox();
			$(cb).checkbox('uncheck');
		}
	},
	values : function(checkbox) {
		var array = new Array();
		for (var i = 0; i < checkbox.length; i++) {
			array.push($(checkbox[i]).val());
		}

		return array;
	}
};

jQuery
		.extend({
			isEmpty : function(str) {
				return str == null || str.length === 0
						|| jQuery.trim(str).length === 0;
			},
			postJSON : function(url, data, callback) {
				jQuery.post(url, data, callback, 'json');
			}
		});

String.prototype.format = function(args) {
	var result = this;
	if (arguments.length > 0) {
		if (arguments.length == 1 && typeof (args) == "object") {
			for ( var key in args) {
				if (args[key] != undefined) {
					var reg = new RegExp("({" + key + "})", "g");
					result = result.replace(reg, args[key]);
				}
			}
		} else {
			for (var i = 0; i < arguments.length; i++) {
				if (arguments[i] != undefined) {
					var reg = new RegExp("({[" + i + "]})", "g");
					result = result.replace(reg, arguments[i]);
				}
			}
		}
	}
	return result;
}

/**
 * 自动补0
 */
app.autoComplete = {
		complete : function(value,bit) {
			 var zero="";
			 while (zero.toString().length<bit) {
				 zero=zero.toString()+"0";
			 }
			 var value=Math.round(parseFloat(value)*(1+zero))/(1+zero);
			 var xsd=value.toString().split(".");
			 if(xsd.length==1){
				 value=value.toString()+"."+zero;
				 return value;
			 }
			 if(xsd.length>1){
				 while (xsd[1].length<bit){
					 xsd[1]=xsd[1].toString()+"0";
				 }
				 return xsd[0]+"."+xsd[1];
			 }
		}
	};